package com.example.study.module;

import com.example.study.common.base.RestResponse;
import com.example.study.utils.FileUtil;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * @author kuotian
 */
@RestController
@RequestMapping("/music")
public class MusicController {

    @GetMapping("/list")
    public RestResponse getMusicList() {
        RestResponse restResponse = new RestResponse();
        String servicePath = FileUtil.basePath() + "music";
        ArrayList arrayList = FileUtil.scanFile(servicePath);
        System.out.println(arrayList);
        restResponse.setData(arrayList);
        restResponse.setCode(10000);
        return restResponse;
    }

    @GetMapping("/listen")
    public ResponseEntity<Resource> listenMusic(@RequestParam("path") String path) {
        return FileUtil.getFileStream(path, MediaType.ALL);
    }
}
